#include "ccl2baseconverter.h"

#include <QDir>
#include <QFile>
#include <QTextStream>

#include <QDebug>

Ccl2BaseConverter::Ccl2BaseConverter()
{
    tagset = Corpus2::get_named_tagset("nkjp");
}

void Ccl2BaseConverter::convert(const QString &inputFile, const QString &outputFile)
{
    reader = Corpus2::TokenReader::create_path_reader("ccl", tagset, inputFile.toStdString());

    QFile outFile(outputFile);
    QTextStream out(&outFile);
    Corpus2::Token *token;
    QString base;
    Corpus2::Sentence::Ptr sentence;

    outFile.open(QIODevice::WriteOnly);

    while(token = reader->get_next_token()){
        out << QString::fromStdString(token->get_preferred_lexeme(tagset).lemma_utf8());
        out << ' ';
    }

    outFile.close();
}

void Ccl2BaseConverter::convertAll(const QString &inputDir, const QString &outputDir)
{
    QDir inDir(inputDir);
    QStringList files(inDir.entryList(QDir::Files));
    int i = 1;
    foreach(const QString &file, files){
        convert(QString("%1/%2").arg(inputDir).arg(file), QString("%1/%2.txt").arg(outputDir).arg(file.left(file.size() - 4)));
        qDebug() << QString("converting %1/%2").arg(i).arg(files.size());
        i++;
    }
}
